package dto

import (
	"math/rand"
	"time"
	"tuowei.com/app/admin/models"
	"tuowei.com/common/dto"
	common "tuowei.com/common/models"
	"tuowei.com/pkg/xlog"
)

type SysCourseGetPageReq struct {
	dto.Pagination `search:"-"`
	Name           string `form:"name" search:"type:contains;column:name;table:sys_course" comment:"课程名称"` // 课程名称
	Status         int    `form:"status" search:"type:exact;column:status;table:sys_course" comment:"状态"`  // 状态
	StartTime      string `search:"type:gte;column:created_at;table:sys_course" form:"beginTime"`
	EndTime        string `search:"type:lte;column:created_at;table:sys_course" form:"endTime"`
	CourseType     string `search:"type:exact;column:course_type;table:sys_course" form:"course_type" json:"course_type" comment:"课程类型"`
	Subject        string `search:"type:exact;column:subject;table:sys_course" form:"subject" comment:"科目"`
	Province       string `form:"province" search:"type:contains;column:province;table:sys_course" comment:"适用地区"`
	IsBindOffline  int    `form:"is_bind_offline" search:"type:exact;column:is_bind_offline;table:sys_course" comment:"是否关联面授课程 1为关联"` // 状态
	SysCourseOrder
}

type SysCourseOrder struct {
	CourseIdOrder  string `search:"type:order;column:course_id;table:sys_course" form:"course_id_sort"`
	CreatedAtOrder string `search:"type:order;column:created_at;table:sys_course" form:"created_at_sort"`
	UpdatedAtOrder string `search:"type:order;column:updated_at;table:sys_course" form:"updated_at_sort"`
}

func (m *SysCourseGetPageReq) GetNeedSearch() interface{} {
	return *m
}

type Step1 struct {
	SerialNum          string  `json:"serial_num" comment:"课程编号" `                                   // 课程编号vd:"@:len($)>0; msg:'课程编号不能为空'"
	Subject            string  `json:"subject" comment:"科目" vd:"@:len($)>0; msg:'请选择科目'"`            // 科目
	Name               string  `json:"name" comment:"课程名称" vd:"@:len($)>0; msg:'课程名称不能为空'"`          // 课程名称
	Cover              string  `json:"cover" comment:"封面" vd:"@:len($)>0; msg:'请上传课程封面'"`            // 封面
	Outline            string  `json:"outline" comment:"大纲"`                                         //大纲
	Price              float64 `json:"price" comment:"价格"`                                           // 价格
	Introduction       string  `json:"introduction" comment:"课程简介" vd:"@:len($)>0; msg:'请用一句话介绍课程'"` //课程简介
	AuditionLink       string  `json:"audition_link" comment:"试听链接地址"`                               //试听链接地址
	Announcement       string  `json:"announcement" comment:"课程公告"`                                  //课程公告
	AuditionCourseName string  `json:"audition_course_name" comment:"试听课程名称"`
	Status             int     `json:"status" comment:"状态"`
	PublicRange        string  `json:"public_range" comment:"公开范围"`
	Province           string  `json:"province" comment:"适用地区"` //适用区域
}

type Step2 struct {
	LecturerId     int           `json:"lecturer_id" comment:"讲师" vd:"$>0; msg:'请选择讲师'"`
	OnlineClassId  int           `json:"online_class_id" comment:"网课班级ID"` //网课班级ID
	Contacts       string        `json:"contacts" comment:"班主任联系"`         //班主任联系
	IsBindOffline  int           `json:"is_bind_offline" comment:"是否关联面授课程"`
	TermOfValidity int           `json:"term_of_validity" comment:"网课有效期"`
	CourseType     string        `json:"course_type"`
	LiveUrl        string        `json:"live_url" gorm:"type:varchar(255);comment:直播地址"`
	StartTime      string        `json:"start_time" gorm:"datetime;comment:直播开始时间"`
	EndTime        string        `json:"end_time" gorm:"datetime;comment:直播结束时间"`
	Telephone      string        `json:"telephone" comment:"联系电话"`
	IsBoutique     int           `json:"is_boutique" comment:"是否设置成精品 1为精品,-1未设置"`
	CourseGift     []*CourseGift `json:"course_gift" comment:"赠送数组"`
	Qq             string        `json:"qq" comment:"客服qq"`
}

type SysCourseInsertReq struct {
	CourseId       int    `json:"course_id" comment:"课程ID"` // 课程ID
	Step1          Step1  `json:"step1" comment:"步骤一"`
	Step2          Step2  `json:"step2" comment:"步骤二"`
	CourseWareJson string `json:"course_ware_json" comment:"课程章节信息"`
	common.ControlBy
}

var letters = []rune("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randSeq(n int) string {
	b := make([]rune, n)
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := range b {
		b[i] = letters[r.Intn(36)]
	}
	return string(b)
}

func (s *SysCourseInsertReq) Generate(model *models.SysCourse) {
	//model.CourseId = s.CourseId
	model.Name = s.Step1.Name
	model.Status = s.Step1.Status
	model.IsBoutique = s.Step2.IsBoutique
	model.Subject = s.Step1.Subject
	model.Cover = s.Step1.Cover
	model.SerialNum = randSeq(6)
	model.Price = s.Step1.Price
	model.Outline = s.Step1.Outline
	model.IsBindOffline = s.Step2.IsBindOffline
	model.Sales = 0
	model.Introduction = s.Step1.Introduction
	model.Announcement = s.Step1.Announcement
	model.AuditionLink = s.Step1.AuditionLink
	model.AuditionCourseName = s.Step1.AuditionCourseName
	model.LecturerId = s.Step2.LecturerId
	model.OnlineClassId = s.Step2.OnlineClassId
	model.Contacts = s.Step2.Contacts
	model.Province = s.Step1.Province
	model.TermOfValidity = s.Step2.TermOfValidity
	model.CreateBy = s.CreateBy
	model.Creator = s.Creator
	model.Telephone = s.Step2.Telephone
	model.PublicRange = s.Step1.PublicRange
	model.CourseType = s.Step2.CourseType
	model.LiveUrl = s.Step2.LiveUrl
	model.Qq = s.Step2.Qq
	model.StartTime, _ = time.ParseInLocation("2006-01-02 15:04:05", s.Step2.StartTime, time.Local)
	model.EndTime, _ = time.ParseInLocation("2006-01-02 15:04:05", s.Step2.EndTime, time.Local)
}

func (s *SysCourseInsertReq) GetId() interface{} {
	return s.CourseId
}

type SysCourseUpdateStatusReq struct {
	Status   int `json:"status" comment:"状态"`
	CourseId int `json:"course_id" comment:"课程ID" vd:"$>0; msg:'课程ID不能为空'"`
	common.ControlBy
}

func (s SysCourseUpdateStatusReq) Generate(model *models.SysCourse) {
	model.Status = s.Status
}

func (s SysCourseUpdateStatusReq) GetId() int {
	return s.CourseId
}

type SysCourseUpdateReq struct {
	CourseId int   `json:"course_id" comment:"课程ID" vd:"$>0; msg:'课程ID不能为空'"` // 课程ID
	Step1    Step1 `json:"step1" comment:"步骤一"`
	Step2    Step2 `json:"step2" comment:"步骤二"`
	Status   int   `json:"status" comment:"状态"`
	common.ControlBy
}

func (s *SysCourseUpdateReq) Generate(model *models.SysCourse) {
	xlog.Pretty(s.Step2)
	if s.CourseId != 0 {
		model.CourseId = s.CourseId
	}
	model.Name = s.Step1.Name
	model.Status = s.Step1.Status
	model.IsBoutique = s.Step2.IsBoutique
	model.Subject = s.Step1.Subject
	model.Cover = s.Step1.Cover
	model.Price = s.Step1.Price
	model.Outline = s.Step1.Outline
	model.Sales = 0
	model.Introduction = s.Step1.Introduction
	model.AuditionLink = s.Step1.AuditionLink
	model.Announcement = s.Step1.Announcement
	model.AuditionCourseName = s.Step1.AuditionCourseName
	model.PublicRange = s.Step1.PublicRange
	model.LecturerId = s.Step2.LecturerId
	model.OnlineClassId = s.Step2.OnlineClassId
	model.IsBindOffline = s.Step2.IsBindOffline
	model.Contacts = s.Step2.Contacts
	model.Province = s.Step1.Province
	model.TermOfValidity = s.Step2.TermOfValidity
	model.UpdateBy = s.UpdateBy
	model.Updater = s.Updater
	model.Telephone = s.Step2.Telephone
	model.CourseType = s.Step2.CourseType
	model.LiveUrl = s.Step2.LiveUrl
	model.Qq = s.Step2.Qq
	model.StartTime, _ = time.ParseInLocation("2006-01-02 15:04:05", s.Step2.StartTime, time.Local)
	model.EndTime, _ = time.ParseInLocation("2006-01-02 15:04:05", s.Step2.EndTime, time.Local)
}

func (s *SysCourseUpdateReq) GetId() interface{} {
	return s.CourseId
}

type UpdateCourseStatusReq struct {
	RoleId int    `json:"roleId" comment:"角色编码"` // 角色编码
	Status string `json:"status" comment:"状态"`   // 状态
	common.ControlBy
}

func (s *UpdateCourseStatusReq) Generate(model *models.SysRole) {
	if s.RoleId != 0 {
		model.RoleId = s.RoleId
	}
	model.Status = s.Status
}

func (s *UpdateCourseStatusReq) GetId() interface{} {
	return s.RoleId
}

type SysCourseByName struct {
	RoleName string `form:"role"` // 角色编码
}

type SysCourseGetReq struct {
	Id int `uri:"id"`
}

func (s *SysCourseGetReq) GetId() interface{} {
	return s.Id
}

type SysCourseDeleteReq struct {
	Ids []int `json:"ids"`
}

func (s *SysCourseDeleteReq) GetId() interface{} {
	return s.Ids
}

// RoleDataScopeReq 角色数据权限修改
type CourseDataScopeReq struct {
	RoleId    int    `json:"roleId" binding:"required"`
	DataScope string `json:"dataScope" binding:"required"`
	DeptIds   []int  `json:"deptIds"`
}

func (s *CourseDataScopeReq) Generate(model *models.SysRole) {
	if s.RoleId != 0 {
		model.RoleId = s.RoleId
	}
	model.DataScope = s.DataScope
	model.DeptIds = s.DeptIds
}

type SysCourseById struct {
	dto.ObjectById
	common.ControlBy
	BuyerId int `json:"buyer_id"`
}

// SysCourseWatch 这玩意儿不需要永久保存入库，直接缓存一个，十五天的观看记录保存时间
type SysCourseWatch struct {
	CourseId  int  `json:"course_id" binding:"required"` // 课程id
	Completed bool `json:"completed"`                    // 是否完成，如果是true，则会清除掉观看记录
	Duration  int  `json:"duration"`                     // 已经观看时长
	ChapterId int  `json:"chapterId" binding:"required"` // 章节id                 // 章节id
}

func (m *SysCourseById) GetNeedSearch() interface{} {
	return *m
}

type CourseGift struct {
	CourseId   int    `json:"course_id" comment:"课程ID"`
	CourseType string `json:"course_type" comment:"课程类型"`
	GiftType   string `json:"gift_type" comment:"赠送类型"`
	ObjIds     string `json:"obj_ids" comment:"对象IDs"`
}
